<template>
  <div>
    <!-- 标题区域 -->
    <van-nav-bar
      title="账号登陆"
      left-arrow
    />
    <!-- 登陆界面 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入用户名"
        :rules="[
        { pattern: /^\w{5,}/,message: '用户名至少包含5个字符' },
        { required: true, message: '请填写用户名' }
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        :rules="[
        { pattern: /^\w{6,}$/, message: '密码至少包含6个字符' },
        { required: true, message: '请填写密码' }
        ]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (value) {
      const res = await login(value)
      setToken(res.body.token)
      console.log(res)
      if (res.status !== 400) {
        this.$toast('登陆成功')
        this.$router.push('/')
      }
    }
  }
}

</script>

<style>
.van-nav-bar__content{
    background-color: #20b97a;
}
.van-nav-bar .van-icon{
    color: #fff;
}
.van-nav-bar__title{
    font-size: 18px;
    color: #fff;
}
.van-button--info{
    background-color: #20b97a;
}
</style>